import { getFileStream } from "@/apis/file";
import { sliceUpload } from "@/common/fileUtils/sliceUpload";
import { IFileUtils, IGCFXCommonFileItem } from "@/gcfx_web/defaultConfig/config";
import { openMessage } from "@/hy_dev_lib/src";
import {
  BROWSER_PREVIEW_FILE_SUFFIX,
  downloadBlobFile,
  getCanFilePreview,
  getFileSuffix,
} from "@/hy_dev_lib/src/utils/fileUtils";
import { UploadChangeParam, UploadFile } from "antd/es/upload";

export const fileVisitBaseURL = SYSTEM_GLOBAL.fileBaseURL.substring(0, SYSTEM_GLOBAL.fileBaseURL.length - 1);

interface IGCFXFileFieldNames {
  uid?: string;
  name?: string;
  size?: string;
  path?: string; // 不带 fileVisitBaseURL 前缀
  addTime?: string;
}

// 将文件对象转换为统一格式
const getFileObj = (file: any = {}, fileFieldNames?: IGCFXFileFieldNames) => {
  const {
    uid: uidField = "tid",
    name: nameField = "titles",
    size: sizeField = "size",
    path: pathField = "urls",
    addTime: addTimeField = "scsj",
  } = fileFieldNames || {};
  let path = file?.[pathField] || file?.urls || file?.fjlj;
  if (!path) return null;
  const appendSlash = !path.startsWith("/");
  path = (appendSlash ? "/" : "") + path;
  return {
    ...file,
    uid: file[uidField] || file.tid || file.uid + "",
    name: file[nameField] || file.name || file.file_name || file.fjmc || "",
    size: file[sizeField] || file.fjdx || file.size,
    url: fileVisitBaseURL + path,
    fjlj: path,
    scsj: file[addTimeField] || file.addtime,
    status: "done",
  } as IGCFXCommonFileItem;
};

const getFileArr = (fileList: any[], fileFieldNames?: IGCFXFileFieldNames) => {
  if (!fileList?.length) return [];
  return fileList.reduce((prev, item) => {
    const file = getFileObj(item, fileFieldNames);
    if (!file) return prev;
    return [...prev, file];
  }, []);
};

const fileUpload = async (info: UploadChangeParam<UploadFile<IGCFXCommonFileItem>>, saveFolder: string) => {
  const { file } = info;
  const res = await sliceUpload(file, saveFolder);
  return getFileObj(res.data?.[0]);
};

const downloadFile = async (
  fileItem: IGCFXCommonFileItem,
  fileName?: string,
  urlFieldName = "url" // 文件地址取哪个字段
) => {
  // @ts-ignore
  if (!fileItem?.[urlFieldName])
    return openMessage({
      type: "error",
      content: "没有可下载的文件！.",
    });
  // @ts-ignore
  const res = await getFileStream(fileItem?.[urlFieldName]);
  const blob = new Blob([res]);
  downloadBlobFile(blob, fileName || fileItem.name);
};

const onPreviewFile = (fileItem: IGCFXCommonFileItem) => {
  const { tid, name, url } = fileItem;
  const suffix = getFileSuffix(undefined, name);
  // 不能预览的文件 直接下载
  if (!getCanFilePreview(name)) return downloadFile(fileItem, name);
  if (BROWSER_PREVIEW_FILE_SUFFIX.includes(suffix) && url)
    // return window.open("/down" + fjlj)
    return window.open(url);
  // if (!ylwjlx) {
  //   //生成获取文件预览地址
  //   const { data } = await getGenerateViewerPath(url)
  //   //保存预览地址
  //   await saveViewerUrl(tid, data)
  //   window.open(GCFX_WEB_CONFIG.fileVisitBaseURL + data, "_blank")
  // } else {
  //   window.open(GCFX_WEB_CONFIG.fileVisitBaseURL + ylwjlx, "_blank")
  // }
};

const fileUtils: IFileUtils = {
  fileUpload,
  onPreviewFile,
  downloadFile,
  getFileObj,
  getFileArr,
};

export default fileUtils;
